import util from '@/utils/test.js'

// 合并数组
const mergeTopic = function(arr) {
  return arr.reduce((pre, cur) => pre.concat(cur.value), [])
}

const state = {
  test_count: 0, // 试卷的总题数
  test_parts: [], // 试卷部分的数据
  test_duration: 0, // 开始作答的时长
  test_answer: [], // 试卷的试题答案
  test_sheet: [], // 试卷的试题卡状态
  topic_index: 0, // 试题的下标
  topic_id: 0, // 试题的id
  topic_collected: 0 // 试题的收藏状态
}
const Mutation = {
  SET_TEST_INIT: 'SET_TEST_INIT',
  SET_TEST_DURATION: 'SET_TEST_DURATION',
  SET_TEST_ANSWER: 'SET_TEST_ANSWER',
  SET_TEST_STATE_DEFAULT: 'SET_TEST_STATE_DEFAULT',
  SET_TOPIC_PREV: 'SET_TOPIC_PREV',
  SET_TOPIC_NEXT: 'SET_TOPIC_NEXT',
  SET_TOPIC_CURRENT: 'SET_TOPIC_CURRENT',
  SET_TOPIC_COLLECTED: 'SET_TOPIC_COLLECTED'
}

const mutations = {
  // 考试基础信息
  [Mutation.SET_TEST_INIT](state, data) {
    state.test_count = data.total_count
    state.test_parts = data.parts
    const topicList = mergeTopic(state.test_parts) // 合并试卷部分的试题
    state.topic_id = topicList[state.topic_index].topic_id //初始化id
    // 设置答题卡的初始化状态
    topicList.forEach(item => {
      state.test_sheet.push({
        has_answered: 0
      })
    })
    // 设置试卷答案的数据模型
    state.test_parts.forEach(item => {
      state.test_answer.push({
        part_id: item.id,
        data: []
      })
    })
  },
  // 考试的计时时间
  [Mutation.SET_TEST_DURATION](state, time) {
    state.test_duration = time
  },
  // 试题上一题
  [Mutation.SET_TOPIC_PREV](state) {
    state.topic_index -= 1
    let topicArr = mergeTopic(state.test_parts)
    // 试题的id
    state.topic_id = topicArr[state.topic_index].topic_id
  },
  // 试题下一题
  [Mutation.SET_TOPIC_NEXT](state) {
    // 设置答题卡的状态
    state.test_sheet[state.topic_index].has_answered = 1
    // 前进一步
    state.topic_index += 1
    let topicArr = mergeTopic(state.test_parts)
    // 试题的id
    state.topic_id = topicArr[state.topic_index].topic_id
  },
  // 切换到当前的试题
  [Mutation.SET_TOPIC_CURRENT](state, index) {
    state.topic_index = index
    let topicArr = mergeTopic(state.test_parts)
    state.topic_id = topicArr[state.topic_index].topic_id
  },
  // 设置当前的试题选择的答案
  [Mutation.SET_TEST_ANSWER](state, data) {
    let exam_answer = state.test_answer
    const answerPartId = data.part_id
    const answerOpention = data.opention
    exam_answer.forEach(parts => {
      if (parts.part_id == answerPartId) {
        if (util.isEmpty(parts.data)) {
          parts.data.push(answerOpention)
        } else {
          let nmu = parts.data.findIndex(item => {
            return item.topic_id == answerOpention.topic_id
          })
          if (nmu == -1) {
            parts.data.push(answerOpention)
          } else {
            parts.data[nmu] = answerOpention
          }
        }
      }
    })
  },
  // 设置收藏
  [Mutation.SET_TOPIC_COLLECTED](state, status) {
    state.topic_collected = status
  },
  // 销毁页面时初始化state默认状态
  [Mutation.SET_TEST_STATE_DEFAULT](state) {
    state.test_count = 0
    state.test_parts = []
    state.test_duration = 0
    state.test_answer = []
    state.test_sheet = []
    state.topic_index = 0
    state.topic_id = 0
    state.topic_collected = 0
  }
}

const Action = {
  SET_TEST_INIT: 'SET_TEST_INIT'
}
const actions = {
  // 考试基础信息
  [Action.SET_TEST_INIT]({ commit }, data) {
    commit(Mutation.SET_TEST_INIT, data)
  }
}

const getters = {
  topic_info: state => {
    const topicList = mergeTopic(state.test_parts)
    const topicId = state.topic_id
    let topicData = topicList.find(item => item.topic_id == topicId)
    return topicData
  },
  exam_part_id: () => {
    const parts = state.test_parts
    const topicId = state.topic_id
    let partsId = 0
    parts.forEach(element => {
      element.value.forEach(item => {
        if (item.topic_id === topicId) return (partsId = element.id)
      })
    })
    return partsId
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  Action,
  actions,
  getters
}
